//
//  DataCenter+Cube.m
//  bsl-sdk
//
//  Created by Fanty on 13-12-2.
//
//

#import "DataCenter.h"
#import "Modules.h"

@implementation DataCenter

+(DataCenter*)defaultCenter{
    static DataCenter *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[DataCenter alloc] init];
    });
    return sharedInstance;
}


-(NSArray*)uninstallModules{
    NSMutableArray* result=[[NSMutableArray alloc] initWithCapacity:2];
    //下载模块九宫格重复,先便利self.installMoudules
    NSArray* serviceModules=self.serviceModules;
    
    for (CubeModel* model in serviceModules){
        //已安装列表不包含，则为未安装模块
        CubeModel* _model=[self finidInstallCubeModule:model.identifier];
        if(_model==nil){
            [result addObject:model];
        }
      }
    return result;
}

-(NSArray*)updatableModules{
    NSMutableArray* result=[[NSMutableArray alloc] initWithCapacity:2];
    
    NSArray* serviceModules=self.serviceModules;
   
    for(CubeModel* model in serviceModules){
        CubeModel* __model =[self finidInstallCubeModule:model.identifier];
        if(__model!=nil){
            if(model.build>__model.build){
                model.moduleType=CubeMoudleStatusCanUpdate;
                [result addObject:model];//改：_model
            }
        }
    }
    
    return result;
}

-(NSArray*)allModules{
    NSMutableArray* result=[[NSMutableArray alloc] initWithCapacity:2];
    
    NSArray* serviceModules=self.serviceModules;
    for(CubeModel* model in serviceModules){
        CubeModel* __model =[self finidInstallCubeModule:model.identifier];
         if(__model!=nil){
            if(model.build>__model.build){
                __model.moduleType=CubeMoudleStatusCanUpdate;
            }
            [result addObject:__model];
        }
        else{
            model.moduleType=CubeMoudleStatusNone;
            [result addObject:model];

        }
        
      
    }
    return result;

}



-(CubeModel*)finidInstallCubeModule:(NSString*)identifier{
    for (CubeModel* model in self.installMoudules){
        if([model.identifier isEqualToString:identifier]){
             return model;
        }
    }
    
    return nil;
}

-(CubeModel*)findMainCubeModule{
    for (CubeModel* model in self.installMoudules){
        if(model.mainModule)return model;
    }
    return nil;

}


-(CubeModel*)finidServiceCubeModule:(NSString*)identifier{
    for (CubeModel* model in self.serviceModules){
        if([model.identifier isEqualToString:identifier]){
            return model;
        }
    }
    
    return nil;
    
}


@end
